package _1_常用数据类型_关键字;


import org.junit.Test;

// import javax.xml.bind.DatatypeConverter; // jdk11去掉了
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Created by bpzj on 2019-01-22.
 */

public class CopyByteArray {

    // 两个 byte[] 合并到一个
    // 两种方法
    public static void main(String[] args) throws IOException {
        byte[] bytes1 = "liu_".getBytes();
        byte[] bytes2 = "ya_min".getBytes();

        // 方法1
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            byte[] bytes3 = new byte[bytes1.length + bytes2.length];
            System.arraycopy(bytes1, 0, bytes3, 0, bytes1.length);
            System.arraycopy(bytes2, 0, bytes3, bytes1.length, bytes2.length);
        }

        System.out.println(System.currentTimeMillis() - start);

        // 方法2
        long start2 = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            output.write(bytes1);
            output.write(bytes2);
            byte[] out = output.toByteArray();
        }
        System.out.println(System.currentTimeMillis() - start2);

    }

    // byte[] 字节数组截取
    @Test
    public void splitBytes() {
        // byte[] bytes = DatatypeConverter.parseHexBinary("F53E0500B31400002B0F0000F31400007032690F874897A343481400230046B85449DE4E63BE0000");

        // ByteBuffer byteBuffer = ByteBuffer.wrap(bytes,20,10);
        // byte[] bytes1 = Arrays.copyOfRange(bytes, 10, 20 );

        // System.out.println(DatatypeConverter.printHexBinary(bytes1));

    }


}
